package com.yin.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

import com.yin.service.UserService;

/**
*@author LSY
*Time: 2018年12月27日下午1:56:28
*
*
*/
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

	@Bean
	protected UserDetailsService userDetailsService() {
		return new UserService();
	}
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
			.csrf()
			.disable()
			.authorizeRequests()
			.anyRequest()
			.authenticated()
			.and()
				.formLogin()
				.loginPage("/login")
				.failureUrl("/login?error")
				.permitAll()//登录、错误界面可以直接访问
			.and()
				.logout()//注销请求
				.permitAll();//直接访问
	}
	//密码编码问题
	@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    	auth.userDetailsService(userDetailsService()).passwordEncoder(NoOpPasswordEncoder.getInstance());
	}
}
